package cn.mju.tankfighterbackend.config.security;

import cn.mju.tankfighterbackend.dao.CustomerUserRepository;
import cn.mju.tankfighterbackend.entity.CustomerUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    CustomerUserRepository customerUserRepository;




    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        CustomerUser user = customerUserRepository.getUserByUserName(username);
        if(user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }

        log.debug(user+" login");
        return new User(username,user.getPassword(),
                AuthorityUtils.createAuthorityList(user.getAuthoritiesStrings()));
    }
}
